package org.mparts.client.ui.editor;

import org.mparts.client.ui.events.SaveEvent;
import org.mparts.client.ui.events.ShowPartitemEvent;
import org.mparts.shared.MpartsRequestFactory;
import org.mparts.shared.proxy.NewsProxy;

import com.google.gwt.editor.client.Editor;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HasHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.google.web.bindery.requestfactory.gwt.client.RequestFactoryEditorDriver;
import com.sencha.gxt.widget.core.client.Window;
import com.sencha.gxt.widget.core.client.event.HideEvent;
import com.sencha.gxt.widget.core.client.event.HideEvent.HideHandler;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.ShowEvent;
import com.sencha.gxt.widget.core.client.event.ShowEvent.ShowHandler;
import com.sencha.gxt.widget.core.client.form.DateField;
import com.sencha.gxt.widget.core.client.form.Field;
import com.sencha.gxt.widget.core.client.form.HtmlEditor;

public class NewsEditor implements Editor<NewsProxy>, HasHandlers,
		INewsEditorView {

	public interface Binder extends UiBinder<Widget, NewsEditor> {
	}

	public interface Driver extends
			RequestFactoryEditorDriver<NewsProxy, NewsEditor> {
	}

	@UiField
	DateField newsDate;

	@UiField
	HtmlEditor content;


	@UiField
	Window dialog;

	private EventBus eventBus;

	// @Inject
	// Binder uiBinder;
	//
	// @Inject
	private Driver driver;

//	private MpartsRequestFactory rf;

	private INewsEditorPresenter presenter;

	@Inject
	public NewsEditor(EventBus eventBus,
			Binder uiBinder, Driver driver) {
//		this.rf = rf;
		this.eventBus = eventBus;
		dialog = (Window) uiBinder.createAndBindUi(this);
		this.driver = driver;

		
		dialog.addShowHandler(new ShowHandler() {
			
			@Override
			public void onShow(ShowEvent event) {
				fireEvent(new ShowPartitemEvent(true));				
			}
		});
		
		dialog.addHideHandler(new HideHandler() {
			
			@Override
			public void onHide(HideEvent event) {
				fireEvent(new ShowPartitemEvent(false));				
			}
		});
		

	}

	@Override
	public void fireEvent(GwtEvent<?> event) {
		eventBus.fireEvent(event);
	}

	// @Override
	// public HandlerRegistration addSaveEventHandler(SaveEventHandler handler)
	// {
	// return eventBus.addHandler(SaveEvent.TYPE, handler); }

	@Override
	public Window getDialog() {
		return dialog;
	}

	@UiHandler("save")
	public void onSave(SelectEvent event) {
		presenter.saveNews();
	}

	@UiHandler("cancel")
	public void onCancel(SelectEvent event) {
		dialog.hide();
	}

	@Override
	public void clearFields() {
		clearField(newsDate);
		content.clear();
	}
	
	private void clearField( Field<? extends Object> field ) {
		field.clear();
		field.clearInvalid();		
	}


	@Override
	public Driver initDriver(MpartsRequestFactory rf) {
		driver.initialize(rf, this);
		return driver;
	}

	@Override
	public void setPresenter(INewsEditorPresenter presenter) {
		this.presenter = presenter;
		
	}

	@Override
	public Driver getDriver() {
		return driver;
	}

}
